package com.example.hotel_lxy.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hotel_lxy.R;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.DialogReserveBean;
import com.example.hotel_lxy.bean.EventBusBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.ui.activity.LoginActivity;
import com.example.hotel_lxy.ui.activity.SetUpReserveActivity;
import com.example.hotel_lxy.ui.activity.SetUpZeroPointReserveActivity;
import com.example.hotel_lxy.utils.DatePhotoUtils;
import com.example.hotel_lxy.utils.JurisdictionUtils;
import com.example.hotel_lxy.utils.SPUtils;
import com.example.hotel_lxy.wxapi.WXShare;
import com.ysxsoft.common_base.base.RBaseAdapter;
import com.ysxsoft.common_base.base.RViewHolder;
import com.ysxsoft.common_base.utils.DisplayUtils;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.ysxsoft.common_base.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import okhttp3.Call;


/**
 * 零点预定信息
 * create by Sincerly on 9999/9/9 0009
 **/
public class ZeroInformationDialog extends Dialog {
    private Context mContext;
    private TextView text_ft;
    private TextView  text_td,text_set;
    private  static String id;
    RecyclerView rv_over;
    private ImageView back;
    private Bitmap bitmap;
    private List<DialogReserveBean.DataBean.ListBean> listBeans=new ArrayList<>();
    private String director_id;
    private LinearLayout line1;
    private static String data;
    private WXShare wxShare;
    boolean modifyall;
    boolean modifypersonal ;

    boolean retreatall ;
    boolean retreatpersonal;

    boolean overturnall ;
    boolean overturnpersonal;
    public ZeroInformationDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    private View init() {
        View view = View.inflate(mContext, R.layout.dialog_zero_information, null);
        rv_over=view.findViewById(R.id.rv_over);
        text_ft=view.findViewById(R.id.text_ft);
        text_td=view.findViewById(R.id.text_td);
         text_set=view.findViewById(R.id.text_set);
        line1=view.findViewById(R.id.line1);
        wxShare = new WXShare(mContext);
        back=view.findViewById(R.id.back);
        String string6 = SPUtils.get(getContext(), "hotel_mobile", "").toString();
        String string7 = SPUtils.get(getContext(), "hotel_addr", "").toString();
        String string = SPUtils.get(getContext(), "hotel", "").toString();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wxShare.shareText("客户，您好！"+string+"已为您预订"+data+"\n" +
                        "电话："+string6+"\n" +
                        "地址："+string7+"。");
            }
        });

        //修改
        modifyall = JurisdictionUtils.getModifyall(mContext);
        modifypersonal = JurisdictionUtils.getModifypersonal(mContext);
        //退订
        retreatall = JurisdictionUtils.getRetreatall(mContext);
        retreatpersonal = JurisdictionUtils.getRetreatpersonal(mContext);

        //翻台
         overturnall = JurisdictionUtils.getOverturnall(mContext);
         overturnpersonal = JurisdictionUtils.getOverturnpersonal(mContext);


        text_ft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(overturnall){
                    TurntableDialog.show(mContext,null).setListener(new TurntableDialog.OnDialogClickListener() {
                        @Override
                        public void onPositiveClick(String s) {
                            if(!s.equals("")){
                                showDataSeat();
                            }
                        }
                    });
                }else{
                    if(overturnpersonal){
                        boolean getuserid = JurisdictionUtils.getuserid(mContext, director_id);
                        if(getuserid){
                            TurntableDialog.show(mContext,null).setListener(new TurntableDialog.OnDialogClickListener() {
                                @Override
                                public void onPositiveClick(String s) {
                                    if(!s.equals("")){
                                        showDataSeat();
                                    }
                                }
                            });
                        }else{
                            ToastUtils.longToast(mContext,"当前账号没有操作权限哦");
                            return;
                        }
                    }else{
                        ToastUtils.longToast(mContext,"当前账号没有操作权限哦");
                        return;
                    }
                }


            }
        });
        text_td.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    if(retreatall){
                        UnsubscribeDialog.show(mContext,null).setListener(new UnsubscribeDialog.OnDialogClickListener() {
                            @Override
                            public void onPositiveClick(String s) {
                                if(!s.equals("")){
                                    showDataSeatTd(2);
                                }

                            }
                        });
                    }else{
                        if(retreatpersonal){
                            boolean getuserid = JurisdictionUtils.getuserid(mContext, director_id);
                            if(getuserid){
                                UnsubscribeDialog.show(mContext,null).setListener(new UnsubscribeDialog.OnDialogClickListener() {
                                    @Override
                                    public void onPositiveClick(String s) {
                                        if(!s.equals("")){
                                            showDataSeatTd(2);
                                        }

                                    }
                                });
                            }else{
                                ToastUtils.longToast(mContext,"当前账号没有操作权限哦");
                                return;
                            }
                        }else{
                            ToastUtils.longToast(mContext,"当前账号没有操作权限哦");
                            return;
                        }
                    }
            }
        });
        text_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(modifyall){
                    SetUpZeroPointReserveActivity.start(id);
                    dismiss();
                }else{
                    if(modifypersonal){
                        boolean getuserid = JurisdictionUtils.getuserid(mContext, director_id);
                        if(getuserid){
                            SetUpZeroPointReserveActivity.start(id);
                            dismiss();
                        }else{
                            ToastUtils.longToast(mContext,"当前账号没有操作权限哦");
                            return;
                        }
                    }else{
                        ToastUtils.longToast(mContext,"当前账号没有操作权限哦");
                        return;
                    }
                }
            }
        });
        showDataRv();
        return view;
    }

    private void showDataSeatTd(int i) {
        OkHttpUtils.post()
                .url(Api.POST_ZERO_SEAT)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .addParams("id", id+"")
                .addParams("type", i+"")
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "翻台退订信息onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "翻台退订信息onResponse   " + response);
                        BeanBean beanBean=JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("0")){
                            EventBusBean eventBusBean=new EventBusBean();
                            eventBusBean.setStarttime("");
                            eventBusBean.setType(9);
                            EventBus.getDefault().post(eventBusBean); //普通事件发布
                            ToastUtils.show(mContext,beanBean.getMsg());
                            dismiss();
                        }else if(beanBean.getCode().equals("4")){
                            SPUtils.put(mContext,"token","");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                           mContext. startActivity(intent);
                            return;
                        }
                        else{
                            EventBusBean eventBusBean=new EventBusBean();
                            eventBusBean.setStarttime("");
                            eventBusBean.setType(9);
                            EventBus.getDefault().post(eventBusBean); //普通事件发布
                            ToastUtils.show(mContext,beanBean.getMsg());
                            dismiss();
                        }

                    }
                });
    }

    private void showDataSeat() {
        OkHttpUtils.post()
                .url(Api.POST_ZERO_OVER)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .addParams("id", id+"")
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "入座预定信息onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "入座预定信息onResponse   " + response);

                        BeanBean beanBean= JsonUtils.parseByGson(response, BeanBean.class);
                        EventBusBean eventBusBean=new EventBusBean();
                        eventBusBean.setStarttime("");
                        eventBusBean.setType(9);
                        EventBus.getDefault().post(eventBusBean); //普通事件发布
                        if(beanBean.getCode().equals("0")){
                            ToastUtils.show(mContext,beanBean.getMsg());
                            dismiss();
                        }else  if(beanBean.getCode().equals("4")){
                            SPUtils.put(mContext,"token","");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                            return;
                        }
                            else{
                            ToastUtils.show(mContext,beanBean.getMsg());
                            dismiss();
                        }

                    }
                });
    }

    private void showDataRv() {
        OkHttpUtils.post()
                .url(Api.POST_ORDER_MSG)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .addParams("id", id+"")
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "翻台预定信息onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "翻台预定信息onResponse   " + response);
                        DialogReserveBean dialogReserveBean = JsonUtils.parseByGson(response, DialogReserveBean.class);
                        BeanBean beanBean=JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("0")){

                            director_id=dialogReserveBean.getData().getList().getDirector_id()+"";
                            if(modifyall){
                                text_set.setBackgroundDrawable(mContext.getResources().getDrawable(R.mipmap.img_xg_yes));
                            }else{
                                if(modifypersonal){
                                    boolean getuserid = JurisdictionUtils.getuserid(mContext, director_id);
                                    if(getuserid){
                                        text_set.setBackgroundDrawable(mContext.getResources().getDrawable(R.mipmap.img_xg_yes));
                                    }else{
                                        text_set.setBackgroundDrawable(mContext.getResources().getDrawable(R.mipmap.img_xg_no));
                                    }
                                }else{
                                    text_set.setBackgroundDrawable(mContext.getResources().getDrawable(R.mipmap.img_xg_no));
                                }
                            }


                            if(retreatall){
                                text_td.setBackgroundDrawable(mContext.getResources().getDrawable(R.mipmap.img_td_yes));
                            }else{
                                if(retreatpersonal){
                                    boolean getuserid = JurisdictionUtils.getuserid(mContext, director_id);
                                    if(getuserid){
                                        text_td.setBackgroundDrawable(mContext.getResources().getDrawable(R.mipmap.img_td_yes));
                                    }else{
                                        text_td.setBackgroundDrawable(mContext.getResources().getDrawable(R.mipmap.img_td_no));
                                    }
                                }else{
                                    text_td.setBackgroundDrawable(mContext.getResources().getDrawable(R.mipmap.img_td_no));
                                }
                            }

                            if(overturnall){
                                text_ft.setBackgroundDrawable(mContext.getResources().getDrawable(R.mipmap.img_ft_yes));
                            }else{
                                if(overturnpersonal){
                                    boolean getuserid = JurisdictionUtils.getuserid(mContext, director_id);
                                    if(getuserid){
                                        text_ft.setBackgroundDrawable(mContext.getResources().getDrawable(R.mipmap.img_ft_yes));
                                    }else{
                                        text_ft.setBackgroundDrawable(mContext.getResources().getDrawable(R.mipmap.img_ft_no));
                                    }
                                }else{
                                    text_ft.setBackgroundDrawable(mContext.getResources().getDrawable(R.mipmap.img_ft_no));
                                }
                            }

//                            String string2 = SPUtils.get(mContext, "id", "").toString();
//                            if(director_id.equals(""+string2)){
//                                text_ft.setVisibility(View.VISIBLE);
//                                text_set.setVisibility(View.VISIBLE);
//                                text_td.setText("退订");
//                                back.setVisibility(View.VISIBLE);
//                            }else{
//                                text_ft.setVisibility(View.GONE);
//                                text_set.setVisibility(View.GONE);
//                                text_td.setText("关闭");
//                                back.setVisibility(View.GONE);
//                            }
                            rv_over.setLayoutManager(new LinearLayoutManager(mContext));
                            DialogReserveBean.DataBean.ListBean list = dialogReserveBean.getData().getList();
                            listBeans.add(list);
                            RBaseAdapter<DialogReserveBean.DataBean.ListBean> storeDateDialogBeanRBaseAdapter=new RBaseAdapter<DialogReserveBean.DataBean.ListBean>(mContext,
                                    R.layout.dialog_item_history_order,listBeans) {
                                @Override
                                protected void fillItem(RViewHolder holder,DialogReserveBean.DataBean.ListBean item, int position) {
                                    TextView text_elegant=holder.itemView.findViewById(R.id.text_elegant);
                                    TextView text_name=holder.itemView.findViewById(R.id.text_name);
                                    TextView text_mobile=holder.itemView.findViewById(R.id.text_mobile);
                                    TextView text_people=holder.itemView.findViewById(R.id.text_people);
                                    TextView text_end_time=holder.itemView.findViewById(R.id.text_end_time);
                                    TextView text_content=holder.itemView.findViewById(R.id.text_content);
                                    TextView text_yu_name=holder.itemView.findViewById(R.id.text_yu_name);
                                    TextView text_addtime=holder.itemView.findViewById(R.id.text_addtime);
                                    text_elegant.setText(""+item.getElegant());
                                    if(item.getSex()==1){
                                        text_name.setText(""+item.getName()+"先生");
                                    }else{
                                        text_name.setText(""+item.getName()+"女士");
                                    }
                                    text_mobile.setText(""+item.getMobile());
                                    text_people.setText(""+item.getPeople());
                                    text_end_time.setText(""+item.getEnd_time()+item.getTime());
                                    text_content.setText(""+item.getContent());
                                    text_yu_name.setText(""+item.getDirector());
                                    text_addtime.setText(""+item.getAddtime());
                                    text_mobile.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            if (DatePhotoUtils.isPhone(item.getMobile() + "")) {
                                                DialPhoneDialog.show(mContext,null,item.getMobile()+"");
                                            } else {
                                                com.luck.picture.lib.tools.ToastUtils.s(mContext, "手机号有误");
                                            }
//
//                                            Intent intent = new Intent();
//                                            intent.setAction(Intent.ACTION_DIAL);
//                                            intent.setData(Uri.parse("tel:"+item.getMobile()));
//                                            mContext.startActivity(intent);
                                        }
                                    });
                                }

                                @Override
                                protected int getViewType(DialogReserveBean.DataBean.ListBean item, int position) {
                                    return 0;
                                }
                            };
                            rv_over.setAdapter(storeDateDialogBeanRBaseAdapter);
                        }
                        else if(beanBean.getCode().equals("4")){
                            SPUtils.put(mContext,"token","");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                        }
                        else{
                            ToastUtils.show(mContext,beanBean.getMsg());
                            dismiss();
                        }


                    }
                });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(true);
//        setCancelable(false);
        setContentView(init());
    }

    public void showDialog() {
        if (!isShowing()) {
            show();
            WindowManager.LayoutParams lp = getWindow().getAttributes();
//            lp.height = DisplayUtils.getDisplayWidth(mContext) * 5 / 5;
            lp.width = DisplayUtils.getDisplayWidth(mContext) * 9 / 10;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            getWindow().setAttributes(lp);
            getWindow().setGravity(Gravity.CENTER);
        }
    }

    public static ZeroInformationDialog show(Context context, OnDialogClickListener listener,String ids,String datas) {
        ZeroInformationDialog dialog = new ZeroInformationDialog(context, R.style.BottomDialogStyle);
        dialog.setListener(listener);
        id=ids;
        data=datas;
        dialog.showDialog();
        return dialog;
    }

    public OnDialogClickListener listener;

    public ZeroInformationDialog setListener(OnDialogClickListener listener) {
        this.listener = listener;
        return this;
    }

    public interface OnDialogClickListener {
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick(String s);
    }
}